package com.sikaryofficial.backend.controller;

import com.sikaryofficial.backend.domain.dto.req.ShieldReq;
import com.sikaryofficial.backend.service.IUserShieldService;
import com.sikaryofficial.backend.service.article.cache.ArticleCacheService;
import com.sikaryofficial.common.core.utils.HiResultUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.HiResult;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;
import static com.sikaryofficial.backend.constant.ArticleConstant.SHIELD_BUSINESS_TYPE_ARTICLE;
/**
 * 屏蔽Controller
 *
 * @author qinjinyuan
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/user/shield")
@Api(tags = "用户屏蔽服务")
public class UserShieldController extends BaseController {
    @Autowired
    private IUserShieldService shieldService;
    @Resource
    private ArticleCacheService articleCacheService;
    /**
     * 新增屏蔽
     */
    @Log(title = "屏蔽", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增屏蔽")
    public HiResult<Boolean> add(@Validated  @RequestBody ShieldReq shield) {
        boolean flag = shieldService.saveShield(shield);
        clearConfigCache(shield.getBusinessType());
        return HiResultUtil.success(flag);
    }

    private void clearConfigCache(Integer businessType) {
        if (Objects.nonNull(businessType) && SHIELD_BUSINESS_TYPE_ARTICLE.equals(businessType)) {
            articleCacheService.clearCache();
        }
    }

}
